package com.china08.yxyapi.api.home.room;

import java.text.ParseException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.security.profile.YxyProfile;
import com.china08.yxyapi.service.baijiayun.impl.MeetingRoomServiceImpl;

/**
 * 获取视频会议v1.1
 *
 */
@RestController
public class VideoMeetingv1_1RoomUrlController extends YxyApiBaseController {

    @Autowired
    private MeetingRoomServiceImpl meetingRoomServiceImpl;

    @RequestMapping(value = "/api_yw/home/room/videomeetv1_1/geturl", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel precess(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel)
            throws ParseException {

        reqModel.check();

        RespModel model = new RespModel();

        // 有邀请码进入
        if (StringUtils.isNotBlank(reqModel.code)) {
            model.url = meetingRoomServiceImpl.getMeetingRoomUrlByCode(reqModel.id, reqModel.code, reqModel.userNick);
        } else {// 无邀请码进入
            YxyProfile user = getProfile(request, response);
            if (!StringUtils.isNotBlank(user.getId())) {
                model.inputCode = 1;
                return model;
            }
            model.url = meetingRoomServiceImpl.getMeetingRoomUrlV1_1(reqModel.id, user);
            if (StringUtils.isNotBlank(model.url)) {
                model.inputCode = 0;
            } else {
                model.inputCode = 1;
            }
        }
        return model;
    }

    /**
     * 请求参数
     * 
     */
    protected static class ReqModel extends YxyApiReqModel {
        // 会议id
        public String id;
        // 参加码
        public String code;

        // 参加昵称
        public String userNick;

        public void check() {
            Assert.notNull(StringUtils.defaultIfBlank(id, null), "会议编号不能为空");
        }
    }

    /**
     * 响应参数
     * 
     */
    protected static class RespModel extends YxyApiRespModel {

        // 直播房间地址
        public String url;

        // 是否需要code登录 0-不需要 1-需要
        public Integer inputCode = 1;
    }
}
